<?php
/**
 * 公共Action
 * @author tangbl
 *
 */
class PublicAction extends CommonAction{
	
	/**
	 * 转到应用首页
	 */
	public function index(){
		
		redirect(__APP__);
	}
	
	/**
	 * 会员收藏餐厅
	 */
	public function member_collect(){
		
		$member = getMember();
		$model  = M("MemberCollect");
		$map['dataid']  = $member['id'];
		$map['tableid'] = $member['type'];
		$map['oid']     = $this->_param("oid","intval",0);
		
		$view = $model->where($map)->find();
		
		if($view['id']){
			$this->ajaxReturn(array("status"=>-1,"info"=>'Has a collection of over'));
		}else{
			if($map['oid']){
				
				$map['solevar'] = create_id();
				$map['addtime'] = time();
				$data = M("MemberCollect")->add($map);
				
				$this->ajaxReturn(array("status"=>1,"info"=>'Collection of success!'));
			}else{
				$this->ajaxReturn(array("status"=>-1,"info"=>'Collection failed, please try again later!'));
			}
		}
	}
	
	//添加留言信息
	public function message(){
		
		if(IS_AJAX){
			
			$model = D("Message");
			$data = $model->addMessage($_POST);
			
			if(empty($data)){
				$this->ajaxReturn(array("status"=>-1,"info"=>"Sorry Comments failure"));
			}elseif($data=="-1"){
				$this->ajaxReturn(array("status"=>-1, "info"=>"Only message once a day"));
			}else{
				$this->ajaxReturn(array("status"=>1, "info"=>"Congratulations on a successful message"));
			}
		}
	}
	
	
	/**
	 * 校验数据的唯一性
	 */
	public function checkMobile(){
		
		if(IS_AJAX){
					
			$map['param'] = $this->_param("param","trim","");
			$json = sendRest("MMS/Member/checked",$map);
			
			if(($json['status'])==1){
				exit('y');
			}else{
				exit($json['info']);
			}
		}
	}
	
	/**
	 * 校验数据的唯一性
	 */
	public function isexist(){
		
		$config = C("ACCOUNT_CONFIG");
		$field  = $this->_param("name","trim","");
		$accout = $config[$field];
		
		$func  = $accout['func'];
		$value = $this->_param("param","trim","");
		
		if($func($value)){
			$map['active'] = $config["active"];
			$map["field"]  = $field;
			$map["param"]  = $value;
			
			$json = sendRest("Member/isexist", $map);
			if($json['status'] == 1){
				exit('y');
			}else{
				exit(L($accout['exist']));
			}
		}else{
			exit(L($accout['error']));
		}
	}
	
	
	/**
	 * 注册用户
	 */
	public function reg(){
		
		$config = C("ACCOUNT_CONFIG");
		$field  = $config['field'];
		$accout = $config[$field];
		
		if(IS_AJAX){
			
			$verify = $this->_param('yzm',"trim","");
			if(session('verify') != md5(strtoupper($verify)) ){
				$this->error(L('pbc_validate_code_error'));
			}
			
			$_POST['status'] = 1;
			$_POST['isreg']  = 1;
			$_POST['city']   = D("Member")->getUserCityid();//获取城市id
			$json = sendRest("MMS/Member/add",$_POST);
			
			if($json['status']==1){
				
				$map['email']    = $_POST['username'];
				$map['password'] = $_POST['password'];
				$data = sendRest("MMS/Member/login",$map,'post');
				
				if($data['status']==1){
					$this->loginAfter($data['data']);
				}else{
					$this->ajaxReturn($json);
				}
			}
			$this->ajaxReturn($json);
		}else{
			$this->assign("account",$accout);
			$this->display();
		}
	}
	
	/**
	 * 发送短信
	 */
	public function sendSms(){
	
		if(IS_AJAX){
	
			$value = $this->_param("mobile", "trim",  "");
	
			$code   = rand(1000, 9999);
			session("code",md5($code));
	
			$msg = "感谢您注册k.k.RABBIT,您本次的校验码为[".$code."]！";
	
			$map['mobile']   = $value;
			$map['content']  = $msg;
			$res  = sendRest("RMS/Sms/single",$map);
			$this->ajaxReturn($res);
		}
	}
	
	/**
	 * 找回密码发送短信
	 */
	public function findSms(){
		if(IS_AJAX){
		
			$value = $this->_param("mobile", "trim",  "");
		
			$code   = rand(1000, 9999);
			session("code",md5($code));
		
			$msg = "欢迎使用忘记密码功能,您本次的校验码为[".$code."]！请在页面输入完成验证，如非本人操作，请忽略。";
		
			$map['mobile']   = $value;
			$map['content']  = $msg;
			$res  = sendRest("RMS/Sms/single",$map);
			$this->ajaxReturn($res);
		}
	}
	
	/**
	 * 注册成功后置操作
	 */
	public function regAfter($user,$userid,$message){
		
		$config = C("ACCOUNT_CONFIG");
		$actfld = $config['actfield'];
		$field  = $config['field'];
		
		$member = D("Member");
		
		//激活帐号并保存会员个人信息
		$member->activeSave($user,$actfld);
		
		//绑定会员卡
		$map['userid'] = $userid;
		$map[$field]   = $user['username'];
		$json = sendRest("Vip/get",$map,"post");
		
		//转移cart记录
		$member->formGuestCartToUser($user);
		
		//保存用户session
		$member->setLoginSession($user);
		
		//保存用户登陆信息
		$member->setLoginDetail($user);
		
		//通知用戶
		noticeUser("tipreg",array("id"=>$userid));
		
		//记录注册行为
		behaviorEvent("reg",$user['visitid']);
		
		$referurl = session(C('REFER_URL'));
		if(empty($referurl)){
			session(C('REFER_URL'),null);
			$this->assign("jumpUrl",U('Member/member_edit'));
		}else{
			$this->assign("jumpUrl",$referurl);
		}
		$this->success($message);
	}
	
	/**
	 * 手机注册后置操作
	 */
	public function regMobile($user,$userid){
	
		$this->regAfter($user,$userid,L('pbc_login_regsuccess'));
	}
	
	
	/**
	 * 邮箱注册后置操作
	 */
	public function regEmail($user,$userid){
	
		session(C('USER_AUTH_ACCOUNT'),$userid);
	
		$member = D("Member");
		$email  = $user['username'];
	
		$map['solevar']  = create_id();
		$map['username'] = $email;
		$map['email']    = $email;
		$map['webname']  = get_syscfig_key("webname".LANG_SET);
		$map['sign']     = "tipcode";
		$map['addtime']  = time();
		$map['url']      = HTTP_URL.U('Public/active',"code=".$map['solevar']);
	
		$json = sendRest("Email/single",$map);
		if($json['status']==1){
			M("MemberOpreate")->add($map);
		}
		$this->success(L('pbc_login_regsuccess_tip'),U('Public/verification'));
	}
	
	/**
	 * 用户登陆
	 */
	public function login(){
		
 		$config = C("ACCOUNT_CONFIG");
		$field  = $config['field'];
		$accout = $config[$field];
		
		if(IS_POST){
			
			$json = sendRest("MMS/Member/login",$_POST);
			
			if($json['status']==1){
				$this->loginAfter($json['data']);
					
				$referurl = session(C('REFER_URL'));
				/*if(empty($referurl)){
					$this->assign("jumpUrl",U('Member/index'));
				}else{
					$this->assign("jumpUrl",$referurl);
					session(C('REFER_URL'),null);
				}*/
				$this->assign("jumpUrl",U('Index/my_address'));
				$this->success(L('pbc_login_logsuccess'));
			}else{
				$this->error(L('pbc_validate_pwd_error'));
			}
		}else{
			session_destroy();
			$userid = session(C('USER_AUTH_IDEN'));
			if($userid){
				redirect(U('Member/index'));
			}else{
				$this->assign("account",$accout);
				$this->display();
			} 
		}
	}
	
	/**
	 * 登陆后置操作
	 */
	public function loginAfter($user){
		
		$member = D("Member");
		
		//转移cart记录
		//$member->changUserStatus($user);
		
		//保存用户session
		$member->setLoginSession($user);
		
		//保存用户登陆信息
		$member->setLoginDetail($user);
	}
	
	/**
	 * 注销登陆
	 */
	public function logout() {
		
		$member = D("Member");
		$member->clearLoginSession();
		$this->success(L('pbc_login_logout'),U("Index/index"));
	}
	
	/**
	 * 显示验证码
	 */
    public function verify() {
    	
        $type	=	isset($_GET['type'])?$_GET['type']:'gif';
        import("ORG.Util.Image");
        Image::buildImageVerify(4,1,$type);
    }
    /**
     * 邮件激活帐号
     */
    public function active(){
    	
    	$config = C("ACCOUNT_CONFIG");
    	$field  = $config['field'];
    	$accout = $config[$field];
    	
    	$solevar= $this->_get("code","trim","");
    	$time   = time()-30*60;
    	
    	$model  = M("MemberOpreate");
    	$where['solevar'] = $solevar;
    	$where['isdel']   = 2;
    	$where['addtime'] = array("egt",$time);
    	
    	$rows = $model->where($where)->find();
    	if($rows['id']){
    		
    		$model->save(array("id"=>$rows['id'],"isdel"=>1));
    		$map['email'] = $rows["email"];
    		$user = M("Member")->where($map)->find();
    		if($user['id']){
    			$message = "帐号激活成功";
    			$this->regAfter($user, $user['id'],$message);
    		}else{
    			$this->error("帐号不存在",U("Public/reg"));
    		}
    	}else{
    		$this->error("链接已经失效",U("Public/reg"));
    	}
    }
    
    /**
     * 发送激活邮件
     */
    public function verification(){
    	 
    	$config = C("ACCOUNT_CONFIG");
    	$field  = $config['field'];
    	$accout = $config[$field];
    	
    	if(IS_AJAX){
    		$userid = session(C('USER_AUTH_ACCOUNT'));
    		$model  = D("Member");
    		$user   = $model->find($userid);
    		$email  = $user['username'];
    		
    		$map['solevar']  = create_id();
    		$map['username'] = $email;
    		$map['email']    = $email;
    		$map['sign']     = "tipcode";
    		$map['addtime']  = time();
    		$map['url']      = HTTP_URL.U('Public/active',"code=".$map['solevar']);
    		
    		$json = sendRest("Email/single",$map);
    		if($json['status']==1){
    			M("MemberOpreate")->add($map);
    		}
    		$this->ajaxReturn($json);
    		
    	}else{
    		$this->assign("config",$accout);
    		$this->display();
    	}
    }
    
    /**
     * 手机找回密码
     */
    public function findMobile(){
    	
    	if(IS_POST){
    		
    		$account  = $this->_param("email","trim","");
    		$verify   = $this->_param('yzm',"trim","");
    		$password = $this->_param("password","trim","");
    		
    		if(session('verify') == md5(strtoupper($verify))){
    			
    			session(C("verify"),null);
    			
    			$model = D("Member");
    			$map['mobile']   = $account;
    			$map['isreg']    = 1;
    			
    			$user = $model->where($map)->find();
    			if($user['id']){
    				$data['id'] = $user['id'];
    				$data['password'] = myencrypt($password);
    				$flag = $model->save($data);
    				$this->success(L("pbc_dtis_changepwd_succ"),U("Public/login"));
    			}else{
    				$this->error(L('pbc_validate_no_exsits'));
    			}
    		}else{
    			$this->error("验证不通过");
    		}
    	}else{
    		
    		$config = C("ACCOUNT_CONFIG");
    		$field  = $config['field'];
    		$accout = $config[$field];
    		$this->assign("config",$accout);
    		$this->display();
    	}
    }
    
    /**
     * 邮件找回密码
     */
    public function findEmail(){
    	
    	$config = C("ACCOUNT_CONFIG");
    	$field  = $config['field'];
    	$accout = $config[$field];
    	$this->assign("config",$accout);
    	$this->display();
    }
    
    /**
     * 发送找回密码邮件
     */
    public function sendEmail(){
    
    	if(IS_AJAX){
    		
    		$value    = $this->_param("email","trim","");
    		$verify   = $this->_param('yzm',"trim","");
    		$password = $this->_param("password","trim","");
    		
    		if(session('verify') == md5(strtoupper($verify))){
    			
    			$model = M('Member');
    			$data  = $model->where("username='".$value."'")->find(); 
    			
    			if(empty($data['id'])){
    				$this->error(L('pbc_validate_no_exsits'));
    			}
    			if(!isemail($value)){
    				$this->error(L('pbc_login_email_error'));
    			}
    			
	    		$config = C("ACCOUNT_CONFIG");
	    		$accout = $config["email"];
	    		$func   = $accout["func"];
	    		
	    		$map['solevar']  = create_id();
	    		$map['username'] = $value;
	    		$map['email']    = $value;
	    		$map['webname']  = get_syscfig_key("webname".LANG_SET);
	    		$map['realname'] = getSession("USER_AUTH_MODEL","realname");
	    		$map['addtime']  = time();
	    		$map['url']      = HTTP_URL.U('Public/reset',"code=".$map['solevar']);
	    		$map['title']    = L('pbc_form_ele_pwdemail');
				$map['sign']     = 'tippwd';
				$map['lang']     = getLang();
	    		//$map['content']  = L('pbc_form_ele_click')."：".$map['url'];

	    		$json = sendRest("MMS/Email/single",$map);
	    		if($json['status']==1){
	    			$map['content']  = $password;
	    			M("MemberOpreate")->add($map);
	    		}
	    		$this->ajaxReturn($json);
    		}else{
    			$this->error(L('pbc_login_verificationfail'));
    		}
    	}
    }
    
    /**
     * 邮件找回密码 重新设置密码
     */
    public function reset(){
    
    	$config = C("ACCOUNT_CONFIG");
    	$accout = $config["email"];
    
    	$solevar= $this->_param("code","trim","");
    	$time   = time()-30*60;
    
    	$model  = M("MemberOpreate");
    	$where['solevar'] = $solevar;
    	$where['isdel']   = 2;
    	$where['addtime'] = array("egt",$time);
    
    	$rows = $model->where($where)->find();
    	if($rows['id']){
    
    		$module = M("Member");
    		$map['email'] = $rows["email"];
    		$user = $module->where($map)->find();
    
    		if($user['id']){
    				
    			$password = $rows['content'];
    			$password = myencrypt($password);
    
    			$res = $module->save(array("id"=>$user['id'],"password"=>$password));
    			if($res){
    				$model->save(array("id"=>$rows['id'],"isdel"=>1));
    				$this->success(L('pbc_dtis_changepwd_succ'),U("Public/login"));
    			}else{
    				$this->error(L('pbc_dtis_changepwd_fail'),U('Index/index'));
    			}
    		}else{
    			$this->error(L('pbc_validate_no_exsits'),U("Index/index"));
    		}
    
    	}else{
    		$this->error(L('pbc_validate_url_exsits'),U("Index/index"));
    	}
    }
    
    /**
     * 激活邮箱
     */
    public function activeEmail(){
    	
    	$model  = D("Member");
    	$userid = session(C('USER_AUTH_KEY'));
    	$email  = $model->where("id=".$userid)->getField("email");
    	$module = M("MemberOpreate");
    	
    	if(IS_AJAX){
    		
    		$map['solevar']  = create_id();
    		$map['username'] = session(C('USER_AUTH_ACCOUNT'));
    		$map['email']    = $email;
    		$map['sign']     = "actemail";
    		$map['webname']  = get_syscfig_key("webname".LANG_SET);
    		$map['realname'] = getSession("USER_AUTH_MODEL","realname");
    		$map['addtime']  = time();
    		$map['url']      = HTTP_URL.U('Public/activeEmail',"code=".$map['solevar']);
    		 
    		$json = sendRest("Email/single",$map);
    		if($json['status']==1){
    			$module->add($map);
    		}
    		$this->ajaxReturn($json);
    		
    	}else{
    		 		 
    		$solevar= $this->_get("code","trim","");
    		$time   = time()-30*60;
    		 
    		$where['solevar'] = $solevar;
    		$where['isdel']   = 2;
    		$where['addtime'] = array("egt",$time);
    		 
    		$rows = $module->where($where)->find();
    		if($rows['id'] && $email == $rows['email']){
    			
    			$data['actemail'] = 1;
    			$model->where("id=".$userid)->save($data);
    			$this->success("激活邮箱",U("Member/index"));
    			
    		}else{
    			$this->error("链接失效",U("Member/index"));	
    		}
    	}
    }
    
    /**
     * 激活手机
     */
    public function activeMobile(){
    	
    	$model  = D("Member");
    	$userid = session(C('USER_AUTH_KEY'));
    	$mobile = $model->where("id=".$userid)->getField("mobile");
    	
    	if(IS_POST){
    		
    		$verify = $this->_param('yzm',"trim","");
    		if(session('verify') == md5(strtoupper($verify))){
    			$data['actmobile'] = 1;
    			$model->where("id=".$userid)->save($data);
    			$this->success("激活手机成功",U("Member/index"));
    		}else{
    			$this->error(L('pbc_login_verificationfail'));
    		}
    		
    	}elseif(IS_AJAX){
    		
    		$code   = rand(1000, 9999);
    		session("verify",md5($code));
    		
    		$map['username'] = session(C('USER_AUTH_ACCOUNT'));
    		$map['mobile']   = $mobile;
    		$map['sign']     = "actmobile";
    		$map['solevar']  = $code;
    		$map['addtime']  = time();
    		$map['code']     = $code;
    		 
    		$res  = sendRest("Sms/single",$map);
    		if($res['status'] == 1){
    			M("MemberOpreate")->add($map);;
    		}
    		$this->ajaxReturn($res);
    	}else{
    		$this->display();
    	}
    }

    /**
     * 显示门店
     */
    public function outlet(){
    	
    	$lang    = getLang();
		$options = getDictByKey("Dict_address",$lang);
		$this->assign("json",$options["array"]);
		
		$this->assign("pois",mapAll());
		
		$this->display();
    }
    
    /**
     * 微信登陆
     */
    public function login_wechat(){
    
    	$this->display();
    }
    
    /**
     * 添加地址
     */
    public function add_address(){
    	
    	$model  = M('Address');
    	$member = getMember();
    	

    	$map['ads']       = $this->_param('ads','trim','');
    	$map['detail']    = $this->_param('detail','trim','');
    	$map['address']   = $map['ads'].$map['detail'];
    	$map['userid']    = $member['id']; 
    	$map['tableid']   = $member['type'];      
    	$map['realname']  = $this->_param('realname','trim','');
    	$map['mobile']    = $this->_param('mobile','trim','');
    	$map['city']      = $this->_param('city','intval',0);
    	$map['addressid'] = $this->_param('addressid','intval',0);
    	$map['longitude'] = $this->_param('longitude','trim','');
    	$map['latitude']  = $this->_param('latitude','trim','');
    	$map['addtime']   = time();
    	$map['isdefault'] = 1;	
    	
    	$cat['isdefault'] = 2;
    	$model->where("userid='".$member['id']."'")->save($cat);
    	
    	$id = $model->add($map);
    	if($id){
    		$this->ajaxReturn(array('data'=>$id,'status'=>1));
    	}else{
    		$this->ajaxReturn(array('data'=>$id,'status'=>-1));
    	}
    }

	public function email_template(){
    	$this->display();
    }
}
